<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    button.backTop{
      position: fixed;
      right:50px;
      bottom:100px;
      display: none;
    }
  </style>
</head>
<body style="height: 3000px;">
  <div id="app">
    <button @click="isShow = !isShow">按钮</button>
    <button class="backTop" v-if="isShow" v-back-top="500">回到顶部</button>
  </div>
  <script src="./vue.js"></script>
  <script>
    Vue.directive('back-top', {
      bind(el,binding){
        /* console.log(el)
        console.log(binding) */
        window.onscroll = ()=>{
          let sTop = document.documentElement.scrollTop||document.body.scrollTop;
          console.log(sTop)
          if (sTop > binding.value) {
            el.style.display = 'block'
          } else {
            el.style.display = 'none'
          }
        }
        el.onclick = ()=>{
          window.scrollTo(0,0)
        }
      },
      unbind(){
        window.onscroll = null
        // console.log('卸载了')
      }
    })
    const vm = new Vue({
      el: '#app',
      data:{
        isShow: true
      }
    })
  </script>
</body>
</html>